package com.tencent.crazypenguin.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.tencent.crazypenguin.bean.MUser;
import com.tencent.crazypenguin.bean.MathList;
import com.tencent.crazypenguin.bean.User;
import com.tencent.crazypenguin.dao.UserDao;
import com.tencent.crazypenguin.util.JsonUtil;

import net.sf.json.JSONObject;

public class GetMatchUser extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -152784141922505842L;
	
	private String result;

	@Override
	public String execute() throws Exception {
		
		JSONObject postData = JsonUtil.getPostData();
		JSONObject rspJson = new JSONObject();
		UserDao userDao = new UserDao();
		
		
		MUser mUser = new MUser(postData.getJSONObject("origin").getInt("latitude"),postData.getJSONObject("origin").getInt("longitude"),
				postData.getJSONObject("target").getInt("latitude"),postData.getJSONObject("target").getInt("longitude"));
		
		Map<Long,MUser> matchMap = MathList.getMusers(mUser);
		
		if(matchMap == null|| matchMap.size()==0){
			rspJson.put("match_user_count", 0);
			result = rspJson.toString();
			return "done";
		}
		
		rspJson.put("match_user_count", matchMap.size());
		int i=0;
		for(long id:matchMap.keySet()){
			i++;
			mUser = matchMap.get(id);
			User user = userDao.findById(id, new User());
			JSONObject temp = JSONObject.fromObject(user);
			temp.put("origin", JSONObject.fromObject(mUser.origin));
			temp.put("origin", JSONObject.fromObject(mUser.target));
			rspJson.put("user"+i, temp);
		}
		
		result = rspJson.toString();
		return "done";
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	

}
